package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    Page<Orders> pageQueryOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 用于替换微信支付更新数据库状态的问题
     * @param orderStatus
     * @param orderPaidStatus
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);

    //根据订单id查询订单
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    @Select("select count(*) from orders where status = #{Status}")
    Integer countStatus(Integer Status);

    /**
     * 根据订单状态和下单时间查询订单
     * @return
     */
    @Select("select * from orders where status = #{Status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTime(Integer Status, LocalDateTime orderTime);

    /**
     * 统计某天的营业额
     * @param beginTime
     * @param endTime
     */
    Double sumByTime(LocalDateTime beginTime, LocalDateTime endTime,Integer status);

    /**
     * 计算订单总数和有效订单数
     * @param beginTime
     * @param endTime
     * @param Status
     * @return
     */
    Integer OrderCnt(LocalDateTime beginTime, LocalDateTime endTime, Integer Status);

    List<GoodsSalesDTO> top10(LocalDateTime beginTime, LocalDateTime endTime, Integer status);
}
